import com from "@/utils/common.js"

export default function (meta, vm) {
  function setStates() {
    vm.setState("is_items_container", (args) => {
      // let control = args.data?.control;
      // return control == "table" || control == "object" || control == "form";
      debugger;
      return true;
    });

    vm.setState("is_button_container", (args) => {
      debugger;
      return true;
    });
  }

  return {
    onInit: function () {
      setStates();
      vm.getModel("devs-view-detail_controls").setMethod("compare", (a, b) => {
        if (a.isResource || b.isResource) {
          if (a.isResource && b.isResource) {
            return com.utils.compare(a, b);
          } else {
            if (a.isResource) {
              return -1;
            } else {
              return 1;
            }
          }
        } else {
          return com.utils.compare(a, b);
        }
      });

      vm.on("nodeClick", args => {
        if (args.model == "devs-view-detail_controls") {
          const controlId = args.data.id;
          let items = vm.getData()?.items || [];
          items = items.filter(x => {
            return controlId == x.controlId;
          });
          vm.setData("devs-view-detail_items", {
            "controlId": controlId,
            "control": args.data.control,
            "dataType": args.data.dataType,
            "name": args.data.title || args.data.name,
            "rows": items
          });
          let buttons = vm.getData()?.toolItems || [];
          buttons = buttons.filter(x => {
            return controlId == x.rootName;
          });

          vm.setData("devs-view-detail_toolItems", {
            "controlId": controlId,
            "name": "",
            "rows": buttons
          });
        }
      });

      vm.on("addItemsByTitle", args => {
        let data = vm.getData("devs-view-detail_items");
        if (!data.controlId) {
          vm.utils.message("控件ID不能为空！");
          return false;
        }
        args.data.param = {
          "viewId": vm.getData()?.id,
          "controlId": data.controlId,
          "controlName": data.name,
          "titles": ""
        };
        vm.utils.showDialog("dialog_add_items_by_titles", args);
        return args.promise;
      });

      vm.on("addItemsByType", args => {
        let data = vm.getData("devs-view-detail_items");
        if (!data.controlId) {
          vm.utils.message("控件ID不能为空！");
          return false;
        }
        args.data.param = {
          "viewId": vm.getData()?.id,
          "controlId": data.controlId,
          "controlName": data.name,
          "dataType": data.dataType
        };
        vm.utils.showDialog("dialog_set_ctl_items", args);
        return args.promise;
      });
    }
  }
}